package com.midongtec.mduisdkdemo;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import com.mdad.sdk.mduisdk.AdManager;
import com.mdad.sdk.mduisdk.CommonCallBack;

public class MainActivity extends FragmentActivity {
    public static String appId = "20"; //测试id
    public static String appKey = "666666";
    public static String CUID = "123456789"; //用户id 实际中以用户的实际id来
    private String[] permission = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE};
    private EditText et_address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        AdManager.getInstance(this).enableLog(true);
        if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, permission, 98);
        }
        CUID = getAndroidId(this);
        Log.e("hyw","getOaid(this):"+getOaid(this));
        AdManager.getInstance(this).init(this, appId, CUID, appKey, getOaid(this),new CommonCallBack() {
            @Override
            public void onSuccess(String response) {//初始化成功
                Log.e("hyw", "init onSuccess:" + response);
                //跳转 愉悦赚或者其他任务
            }

            @Override
            public void onFailure(String response) {//初始化失败
                Log.e("hyw", "init onFailure:" + response);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }




    //愉悦赚
    public void yuyuezhuan(View view) {
        AdManager.getInstance(this).setH5Title("愉悦赚");
        AdManager.getInstance(this).openH5Task(this,1);
        //或者   AdManager.getInstance(this).openYyzTask(this);
    }

    //新闻赚
    public void newsearn(View view) {
        AdManager.getInstance(this).setH5Title("新闻赚");
        AdManager.getInstance(this).openH5Task(this,3);
    }

    public void swiperList(View view) {
        AdManager.getInstance(this).setH5Title("愉悦赚-横滑列表");
        AdManager.getInstance(this).openH5Task(this,2);
    }

    public void redbag(View view) {
        AdManager.getInstance(this).setTitleBarGone(true);
        AdManager.getInstance(this).setH5Title("愉悦互动-红包");
        AdManager.getInstance(this).openH5Task(this,5);
    }

    public void cpatasks(View view) {
        AdManager.getInstance(this).openCommonTaskList(this);
    }



    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }

    public static String getOaid(final Context context) {
        String oaid0 = PreferencesUtils.getInstance(context).getString("MD_OAID");
        if (TextUtils.isEmpty(oaid0) && context != null) {
            try {
                MiitHelper miitHelper = new MiitHelper(new MiitHelper.AppIdsUpdater() {
                    @Override
                    public void OnIdsAvailed(@NonNull String oaid, boolean isSupport) {
                        Log.e("hyw", "ids:" + oaid);
                        if (isSupport && !TextUtils.isEmpty(oaid)) {
                            PreferencesUtils preferencesUtils = PreferencesUtils.getInstance(context);
                            preferencesUtils.putString("MD_OAID", oaid);
                        }
                    }
                });
                miitHelper.getDeviceIds(context);
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("hyw", "getOaid Exception:" + e.getMessage());
            }
        }
        return oaid0;
    }

    /**
     * 获得设备的AndroidId
     *
     * @param context 上下文
     * @return 设备的AndroidId
     */
    public static String getAndroidId(Context context) {
        String androidId = PreferencesUtils.getInstance(context).getString("ANDR_ID");
        if (TextUtils.isEmpty(androidId)) {
            try {
                androidId = Settings.Secure.getString(context.getContentResolver(),
                        Settings.Secure.ANDROID_ID);
                if (TextUtils.isEmpty(androidId)) {
                    androidId = getOaid(context);
                }
                PreferencesUtils.getInstance(context).putString("ANDR_ID", androidId);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return androidId;
    }

    public static String getDeviceId(Context context) {
        String id = PreferencesUtils.getInstance(context).getString("IMEI");
        if (!TextUtils.isEmpty(id)) {
            return id;
        }
        //android.telephony.TelephonyManager
        try {
            TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (mTelephony != null && mTelephony.getDeviceId() != null) {
                id = mTelephony.getDeviceId();
            } else {
                //android.provider.Settings;
                id = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
            }
            if (TextUtils.isEmpty(id)) {
                id = getAndroidId(context);
                if (TextUtils.isEmpty(id)) {
                    id = getOaid(context);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            id = getAndroidId(context);
            if (TextUtils.isEmpty(id)) {
                id = getOaid(context);
            }
        }
        PreferencesUtils.getInstance(context).putString("IMEI", id);
        return id;
    }


}
